﻿@inject IHttpService _httpService
@inject NavigationManager NavigationManager
@inject IDataCacheService _dataCacheService
@inject IUserService _userService


<div class="@Class">
    @if (_dataCacheService.IsApp)
    {
        <div class="overflow-x-auto" style="display:flex;">
            @if (Model.IsCurrentUserScoreExist)
            {
                <div class="p-2" style="min-width: 300px;">
                    <UserScoreCard Model="Model.UserScores.FirstOrDefault(s => s.User.Id == Model.CurrentUserId)" OnEdit="OnEdit" ShowAction IsPublic="Model.IsCurrentUserScorePublic" IsDubbing="Model.IsDubbing" />
                </div>
            }
            else
            {
                if (ShowBlankAction)
                {
                    <div class="p-2" style="min-width: 250px;">
                        <div class="rounded shadow-sm  bg-opacity h-100" style=" display: flex; justify-content: center; align-items: center; ">
                            <div class="m-4">

                                <AuthorizeView>
                                    <Authorized>
                                        <CnGalWebSite.Components.Buttons.MasaButton Text="添加游玩记录" OnClick="OnEdit" Icon="mdi-plus" TextStyle />
                                    </Authorized>
                                    <NotAuthorized>
                                        <CnGalWebSite.Components.Buttons.MasaButton Text="登入后添加游玩记录" OnClick="_userService.Login" Icon="mdi-login" TextStyle />
                                    </NotAuthorized>
                                </AuthorizeView>

                            </div>
                        </div>
                    </div>

                }
            }
            @foreach (var item in Items.Where(s => s.User.Id != Model.CurrentUserId).Where(s => s.Socres.IsScored || string.IsNullOrWhiteSpace(s.PlayImpressions) == false).Take(MaxCount))
            {
                <div class="p-2" style="min-width: 300px;">
                    <UserScoreCard Model="item" OnClick="@(()=>OnClickUser(item))" IsDubbing="Model.IsDubbing" />
                </div>
            }
        </div>
    }
    else
    {
        <MSlideGroup ShowArrows="true" Class="slide-group-garp mb-4">
            <ChildContent>
                @if (ShowChart)
                {
                    <MSlideItem>
                        <MDCard Class="pa-2" Outline Style="border:0;background-color:transparent;width:300px;">
                            <TotalScoreCard Model="Model" />
                        </MDCard>
                    </MSlideItem>
                }


                @if (Model.IsCurrentUserScoreExist)
                {
                    <MSlideItem>
                        <MDCard Class="pa-2" Outline Style="border:0;background-color:transparentwidth:300px;">
                            <UserScoreCard Model="Model.UserScores.FirstOrDefault(s=>s.User.Id==Model.CurrentUserId)" OnEdit="OnEdit" ShowAction IsPublic="Model.IsCurrentUserScorePublic" IsDubbing="Model.IsDubbing" />
                        </MDCard>
                    </MSlideItem>
                }
                else
                {
                    if (ShowBlankAction)
                    {
                        <MSlideItem>
                            <div style="width:250px; min-height: 250px;">
                                <MDCard Class="h-100" Style=" display: flex; justify-content: center; align-items: center; ">
                                    <div class="m-4">
                                        <AuthorizeView Context="x">
                                            <Authorized>
                                                <CnGalWebSite.Components.Buttons.MasaButton Text="添加游玩记录" OnClick="OnEdit" Icon="mdi-plus" Rounded />
                                            </Authorized>
                                            <NotAuthorized>
                                                 <CnGalWebSite.Components.Buttons.MasaButton Text="登入后添加游玩记录" OnClick="_userService.Login" Icon="mdi-login" Rounded />
                                            </NotAuthorized>
                                        </AuthorizeView>
                                    </div>
                                </MDCard>
                             </div>
                        </MSlideItem>
                    }

                }

                @foreach (var item in Items.Where(s => s.User.Id != Model.CurrentUserId).Where(s => s.Socres.IsScored || string.IsNullOrWhiteSpace(s.PlayImpressions) == false).Take(MaxCount))
                {
                    <MSlideItem>
                        <div style="width:400px;">

                            <UserScoreCard Model="item" OnClick="@(()=>OnClickUser(item))" IsDubbing="Model.IsDubbing" Clickable />
                                </div>
                    </MSlideItem>
                }
            </ChildContent>

        </MSlideGroup>

    }
</div>

<UserScoreDialog @ref="userScoreDialog" />

@code {
    [CascadingParameter]
    public ErrorHandler ErrorHandler { get; set; }
    [Parameter]
    public PlayedGameOverviewModel Model { get; set; }
    [Parameter]
    public bool ShowChart { get; set; }
    [Parameter]
    public bool ShowBlankAction { get; set; }
    [Parameter]
    public int MaxCount { get; set; } = 9999;

    [Parameter]
    public string Class { get; set; }
    [Parameter]
    public EventCallback OnEdit { get; set; }
    UserScoreDialog userScoreDialog;
    public List<PlayedGameUserScoreModel> Items { get; set; }

    protected override void OnParametersSet()
    {
        Items = Model.UserScores;
    }

    public void OnClickUser(PlayedGameUserScoreModel model)
    {
        if (userScoreDialog != null)
        {
            userScoreDialog.Refresh(true, model, null, 0, Model.IsDubbing);
        }
    }

}
